package api

import (
	"net/http"

	// "GinAdmin/internal/model"
	// "GinAdmin/internal/service"
	"GinAdmin/model"
	"GinAdmin/service"

	"github.com/gin-gonic/gin"
)

// SettingAPI 设置API
type SettingAPI struct {
	settingService service.SettingService
}

// GetSetting 获取设置
// @Summary 获取设置
// @Description 获取指定设置
// @Tags 系统设置
// @Accept json
// @Produce json
// @Security ApiKeyAuth
// @Param key path string true "设置键"
// @Success 200 {object} model.Response
// @Router /setting/{key} [get]
func (a *SettingAPI) GetSetting(c *gin.Context) {
	key := c.Param("key")
	setting, err := a.settingService.GetSetting(key)
	if err != nil {
		c.JSON(http.StatusBadRequest, model.Response{
			Code: 400,
			Msg:  "获取设置失败: " + err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, model.Response{
		Code: 200,
		Msg:  "获取成功",
		Data: setting,
	})
}

// CreateSetting 创建设置
// @Summary 创建设置
// @Description 创建新设置
// @Tags 系统设置
// @Accept json
// @Produce json
// @Security ApiKeyAuth
// @Param data body model.CreateSettingRequest true "设置信息"
// @Success 200 {object} model.Response
// @Router /setting [post]
func (a *SettingAPI) CreateSetting(c *gin.Context) {
	var req model.CreateSettingRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, model.Response{
			Code: 400,
			Msg:  "请求参数错误: " + err.Error(),
		})
		return
	}

	setting, err := a.settingService.CreateSetting(&req)
	if err != nil {
		c.JSON(http.StatusInternalServerError, model.Response{
			Code: 500,
			Msg:  "创建设置失败: " + err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, model.Response{
		Code: 200,
		Msg:  "创建成功",
		Data: setting,
	})
}

// UpdateSetting 更新设置
// @Summary 更新设置
// @Description 更新指定设置
// @Tags 系统设置
// @Accept json
// @Produce json
// @Security ApiKeyAuth
// @Param key path string true "设置键"
// @Param data body model.UpdateSettingRequest true "设置更新信息"
// @Success 200 {object} model.Response
// @Router /setting/{key} [put]
func (a *SettingAPI) UpdateSetting(c *gin.Context) {
	key := c.Param("key")
	var req model.UpdateSettingRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, model.Response{
			Code: 400,
			Msg:  "请求参数错误: " + err.Error(),
		})
		return
	}

	setting, err := a.settingService.UpdateSetting(key, &req)
	if err != nil {
		c.JSON(http.StatusInternalServerError, model.Response{
			Code: 500,
			Msg:  "更新设置失败: " + err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, model.Response{
		Code: 200,
		Msg:  "更新成功",
		Data: setting,
	})
}

// DeleteSetting 删除设置
// @Summary 删除设置
// @Description 删除指定设置
// @Tags 系统设置
// @Accept json
// @Produce json
// @Security ApiKeyAuth
// @Param key path string true "设置键"
// @Success 200 {object} model.Response
// @Router /setting/{key} [delete]
func (a *SettingAPI) DeleteSetting(c *gin.Context) {
	key := c.Param("key")
	if err := a.settingService.DeleteSetting(key); err != nil {
		c.JSON(http.StatusInternalServerError, model.Response{
			Code: 500,
			Msg:  "删除设置失败: " + err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, model.Response{
		Code: 200,
		Msg:  "删除成功",
	})
}

// GetSettingsByGroup 根据分组获取设置
// @Summary 根据分组获取设置
// @Description 根据分组获取设置列表
// @Tags 系统设置
// @Accept json
// @Produce json
// @Security ApiKeyAuth
// @Param group query string false "分组名称"
// @Success 200 {object} model.Response
// @Router /setting/list [get]
func (a *SettingAPI) GetSettingsByGroup(c *gin.Context) {
	group := c.Query("group")
	settings, err := a.settingService.GetSettingsByGroup(group)
	if err != nil {
		c.JSON(http.StatusInternalServerError, model.Response{
			Code: 500,
			Msg:  "获取设置列表失败: " + err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, model.Response{
		Code: 200,
		Msg:  "获取成功",
		Data: settings,
	})
}

// BatchUpdateSettings 批量更新设置
// @Summary 批量更新设置
// @Description 批量更新多个设置
// @Tags 系统设置
// @Accept json
// @Produce json
// @Security ApiKeyAuth
// @Param data body model.BatchUpdateSettingsRequest true "批量设置更新信息"
// @Success 200 {object} model.Response
// @Router /setting/batch [put]
func (a *SettingAPI) BatchUpdateSettings(c *gin.Context) {
	var req model.BatchUpdateSettingsRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, model.Response{
			Code: 400,
			Msg:  "请求参数错误: " + err.Error(),
		})
		return
	}

	if err := a.settingService.BatchUpdateSettings(&req); err != nil {
		c.JSON(http.StatusInternalServerError, model.Response{
			Code: 500,
			Msg:  "批量更新设置失败: " + err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, model.Response{
		Code: 200,
		Msg:  "更新成功",
	})
}
